package com.funnyx.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

@EnableConfigurationProperties(DatasourceProp.class)
public class DatasourceConfig {

  private final DatasourceProp datasourceProp;

  public DatasourceConfig(DatasourceProp datasourceProp) {
    this.datasourceProp = datasourceProp;
  }

  @Bean
  public DataSource getDatasource() {
    DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
    dataSourceBuilder.driverClassName("com.alibaba.druid.pool.DruidDataSource");
    dataSourceBuilder.url(datasourceProp.getUrl());
    dataSourceBuilder.username(datasourceProp.getUsername());
    dataSourceBuilder.password(datasourceProp.getPassword());
    return dataSourceBuilder.build();
  }
}
